set more off   
set scheme s1color

use "$data/calibrationdataset.dta", clear

gen R_cl_ori_cl=R_cl_ori*cl_ori 
gen R_tl_st_ori_st=R_tl_st_ori*st_tl_ori  
gen R_tl_mt_ori_mt= R_tl_mt_ori*mt_tl_ori 
gen R_tl_lt_ori_lt=R_tl_lt_ori*lt_tl_or 



*keep if year==2010

collapse (sum)  nfirms_ce_ex nfirms_ce dip_ce pop emp GDP  ///
g_st_tl_res g_mt_lt_tl_res  g_cl_res st_tl_res  mt_lt_tl_res  cl_res g_lt_tl_ori ///
R_cl_ori_cl R_tl_st_ori_st R_tl_mt_ori_mt R_tl_lt_ori_lt cl_ori st_tl_ori mt_tl_ori lt_tl_ori ///
   nfirms_age0 nfirms_age1 nfirms_age2 nfirms_age3 nfirms_age4 nfirms_age5 nfirms_age6 nfirms_age7 nfirms_age8 nfirms_age9 nfirms_age10 nfirms_age11 nfirms_age12 nfirms_age13 nfirms_age14 nfirms_age15 nfirms_age16 nfirms_age17 ///
  nfirms_ex_age0 nfirms_ex_age1 nfirms_ex_age2 nfirms_ex_age3 nfirms_ex_age4 nfirms_ex_age5 nfirms_ex_age6 nfirms_ex_age7 nfirms_ex_age8 nfirms_ex_age9 nfirms_ex_age10 nfirms_ex_age11 nfirms_ex_age12 nfirms_ex_age13 nfirms_ex_age14 nfirms_ex_age15 nfirms_ex_age16  nfirms_ex_age17 ///
  nfirms_pg_age0 nfirms_pg_age1 nfirms_pg_age2 nfirms_pg_age3 nfirms_pg_age4 nfirms_pg_age5 nfirms_pg_age6 nfirms_pg_age7 nfirms_pg_age8 nfirms_pg_age9 nfirms_pg_age10 nfirms_pg_age11 nfirms_pg_age12 nfirms_pg_age13 nfirms_pg_age14 nfirms_pg_age15 nfirms_pg_age16 nfirms_pg_age17 ///
  nfirms_pg_ex_age0 nfirms_pg_ex_age1 nfirms_pg_ex_age2 nfirms_pg_ex_age3 nfirms_pg_ex_age4 nfirms_pg_ex_age5 nfirms_pg_ex_age6 nfirms_pg_ex_age7 nfirms_pg_ex_age8 nfirms_pg_ex_age9 nfirms_pg_ex_age10 nfirms_pg_ex_age11 nfirms_pg_ex_age12 nfirms_pg_ex_age13 nfirms_pg_ex_age14 nfirms_pg_ex_age15  nfirms_pg_ex_age16 nfirms_pg_ex_age17 ///
 nfirms_pg_ins_age0 nfirms_pg_ins_age1 nfirms_pg_ins_age2 nfirms_pg_ins_age3 nfirms_pg_ins_age4 nfirms_pg_ins_age5 nfirms_pg_ins_age6 nfirms_pg_ins_age7 nfirms_pg_ins_age8 nfirms_pg_ins_age9 nfirms_pg_ins_age10 nfirms_pg_ins_age11 nfirms_pg_ins_age12 nfirms_pg_ins_age13 nfirms_pg_ins_age14 nfirms_pg_ins_age15 nfirms_pg_ins_age16 nfirms_pg_ins_age17 ///
 nfirms_ce_age0     nfirms_ce_age1     nfirms_ce_age2     nfirms_ce_age3     nfirms_ce_age4     nfirms_ce_age5     nfirms_ce_age6     nfirms_ce_age7     nfirms_ce_age8     nfirms_ce_age9     nfirms_ce_age10     nfirms_ce_age11     nfirms_ce_age12     nfirms_ce_age13     nfirms_ce_age14     nfirms_ce_age15     nfirms_ce_age16     nfirms_ce_age17 ///
 nfirms_ce_ex_age0  nfirms_ce_ex_age1  nfirms_ce_ex_age2  nfirms_ce_ex_age3  nfirms_ce_ex_age4  nfirms_ce_ex_age5  nfirms_ce_ex_age6  nfirms_ce_ex_age7  nfirms_ce_ex_age8  nfirms_ce_ex_age9  nfirms_ce_ex_age10  nfirms_ce_ex_age11  nfirms_ce_ex_age12  nfirms_ce_ex_age13  nfirms_ce_ex_age14  nfirms_ce_ex_age15  nfirms_ce_ex_age16  nfirms_ce_ex_age17 ///
nfirms_ce_ins_age0 nfirms_ce_ins_age1 nfirms_ce_ins_age2 nfirms_ce_ins_age3 nfirms_ce_ins_age4 nfirms_ce_ins_age5 nfirms_ce_ins_age6 nfirms_ce_ins_age7 nfirms_ce_ins_age8 nfirms_ce_ins_age9 nfirms_ce_ins_age10 nfirms_ce_ins_age11 nfirms_ce_ins_age12 nfirms_ce_ins_age13 nfirms_ce_ins_age14 nfirms_ce_ins_age15 nfirms_ce_ins_age16 nfirms_ce_ins_age17 ///
dip_ce_age0        dip_ce_age1        dip_ce_age2        dip_ce_age3        dip_ce_age4        dip_ce_age5        dip_ce_age6        dip_ce_age7        dip_ce_age8        dip_ce_age9        dip_ce_age10        dip_ce_age11        dip_ce_age12        dip_ce_age13        dip_ce_age14        dip_ce_age15        dip_ce_age16        dip_ce_age17 ///
debt_ce_age0       debt_ce_age1       debt_ce_age2       debt_ce_age3       debt_ce_age4       debt_ce_age5       debt_ce_age6       debt_ce_age7       debt_ce_age8       debt_ce_age9       debt_ce_age10       debt_ce_age11       debt_ce_age12       debt_ce_age13       debt_ce_age14       debt_ce_age15       debt_ce_age16       debt_ce_age17 ///
va_ce_age0         va_ce_age1         va_ce_age2         va_ce_age3         va_ce_age4         va_ce_age5         va_ce_age6         va_ce_age7         va_ce_age8         va_ce_age9         va_ce_age10         va_ce_age11         va_ce_age12         va_ce_age13         va_ce_age14         va_ce_age15         va_ce_age16         va_ce_age17 ///
DEBT_ce_age0       DEBT_ce_age1       DEBT_ce_age2       DEBT_ce_age3       DEBT_ce_age4       DEBT_ce_age5       DEBT_ce_age6       DEBT_ce_age7       DEBT_ce_age8       DEBT_ce_age9       DEBT_ce_age10       DEBT_ce_age11       DEBT_ce_age12       DEBT_ce_age13       DEBT_ce_age14       DEBT_ce_age15       DEBT_ce_age16       DEBT_ce_age17 ///
R_ce_age0          R_ce_age1          R_ce_age2          R_ce_age3          R_ce_age4          R_ce_age5          R_ce_age6          R_ce_age7          R_ce_age8          R_ce_age9          R_ce_age10          R_ce_age11          R_ce_age12          R_ce_age13          R_ce_age14          R_ce_age15          R_ce_age16          R_ce_age17    ///
R_cl_ce_age0       R_cl_ce_age1       R_cl_ce_age2       R_cl_ce_age3       R_cl_ce_age4       R_cl_ce_age5       R_cl_ce_age6       R_cl_ce_age7       R_cl_ce_age8       R_cl_ce_age9       R_cl_ce_age10       R_cl_ce_age11       R_cl_ce_age12       R_cl_ce_age13       R_cl_ce_age14       R_cl_ce_age15       R_cl_ce_age16       R_cl_ce_age17 ///
 (mean) latitude SD_shock MSD_shock provIstat HICP3  firm_pc firm_size  BCrate_ce SD_lnemp_ce_age0 SD_lndebt_emp_ce_age0 MSD_lndebt_emp_ce_age0 year, by(South) /* year, by(South) */
 ********************************************
 

 
 *Normalize GDP per capita
 egen TGDP=total(GDP)
 egen Temp=total(emp)
gen GDP_PW=GDP/emp/(TGDP/Temp)
gen GDP_PC=GDP/pop



************************************************
* Generate DEBT Guarantee

gen sharedebt_g_mt=g_mt_lt_tl_res/mt_lt_tl_res 
gen sharedebt_g_lt=g_lt_tl_ori/lt_tl_ori
gen sharedebt_g_n1=(g_st_tl_res+ g_mt_lt_tl_res + g_cl_res)/(st_tl_res+ mt_lt_tl_res + cl_res)
gen sharedebt_ng=1-sharedebt_g_n1
label variable sharedebt_ng "Share of non collateralized debt" 
***********************************************************
*Gneerate average interest rates

gen R_tl_av=(R_cl_ori_cl+R_tl_st_ori_st +R_tl_mt_ori_mt+R_tl_lt_ori_lt)/(cl_ori+st_tl_ori+mt_tl_ori+lt_tl_ori)
gen R_cl=R_cl_ori_cl/cl_ori
gen R_st=R_tl_st_ori_st/st_tl_ori
gen R_mt=R_tl_mt_ori_mt/mt_tl_ori
gen R_lt=R_tl_lt_ori_lt/lt_tl_ori


label variable R_cl "Interest rates credit lines (incl. fees)"
label variable R_tl_av "Interest rate  term loans (incl. fees)"

label variable GDP_PC "GDP per capita"
*label variable erate "Employment rate"
label variable SD_shock "Standard Deviation shock"
*label variable NFC_FDI "Loans-GDP ratio Non-financial-corporations" 
label variable provIstat "Province ISTAT code"






************************************************
*Calculate rate
xtset South year
*gen size0V1=0.5*(dip_ce_age0/(nfirms_ce_age0-nfirms_ce_ex_age0))+0.5*(dip_ce_age1/(nfirms_ce_age1-nfirms_ce_ex_age1))
*gen size0V2=dip_ce_age0/(nfirms_ce_age0-nfirms_ce_ex_age0)
*gen size0V3=dip_ce_age0/(0.5*(nfirms_ce_age0-nfirms_ce_ex_age0)+0.5*(nfirms_ce_age1-nfirms_ce_ex_age1))
*gen size0V4=(0.5*dip_ce_age0+0.5*dip_ce_age1)/(0.5*(nfirms_ce_age0-nfirms_ce_ex_age0)+0.5*(nfirms_ce_age1-nfirms_ce_ex_age1))
*gen size0V4=(0.5*dip_ce_age0+0.5*dip_ce_age1)/(0.5*(nfirms_ce_age0-nfirms_ce_ex_age0)+0.5*(nfirms_ce_age1-nfirms_ce_ex_age1))
*gen size0=(dip_ce_age0+dip_ce_age1)/(nfirms_ce_age0-nfirms_ce_ex_age0+nfirms_ce_age1-nfirms_ce_ex_age1)

gen size0=0.5*(dip_ce_age0/(nfirms_ce_age0-nfirms_ce_ex_age0))+0.5*(dip_ce_age1/(nfirms_ce_age1-nfirms_ce_ex_age1))
sum size0*
*replace size0=size0V1


foreach num in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17{
*Exit rate VARIABLE FROM CERVED
gen exit_ce_ex_age`num'=nfirms_ce_ex_age`num'/nfirms_ce_age`num'
gen exit_ce_ins_age`num'=nfirms_ce_ins_age`num'/nfirms_ce_age`num'
*Productivity
*gen va_e_ce_age`num'=(va_ce_age`num'/dip_ce_age`num')/(va_ce_age0/dip_ce_age0)
gen va_e_ce_age`num'=log(va_ce_age`num'/dip_ce_age`num')
*Debt over value added
gen debt_va_ce_age`num'=debt_ce_age`num'/va_ce_age`num'
gen DEBT_va_ce_age`num'=DEBT_ce_age`num'/va_ce_age`num'
*Debt per employee
gen debt_e_ce_age`num'=debt_ce_age`num'/dip_ce_age`num'/(debt_ce_age0/dip_ce_age0)
gen DEBT_e_ce_age`num'=DEBT_ce_age`num'/dip_ce_age`num'/(DEBT_ce_age0/dip_ce_age0)
*Employment size of firms in North and South
local numP=`num'+1
*replace nfirms_ce_age`num'=(nfirms_ce_age`num'+nfirms_ce_age`numP')/2
*replace nfirms_ce_age`num'=nfirms_ce_age`numP'
*gen e_ce_age`num'=(dip_ce_age`num'/nfirms_ce_age`numP')/(dip_ce_age0/nfirms_ce_age1)
*gen e_ce_age`num'=(dip_ce_age`num'/nfirms_ce_age`numP')/(dip_ce_age0/nfirms_ce_age1)
*if `num'<16{
*gen e_ce_age`num'=(dip_ce_age`num'/nfirms_ce_age`numP')/(dip_ce_age0/nfirms_ce_age1)
*}
*gen e_ce_age`num'=log(dip_ce_age`num'/nfirms_ce_age`num')-log(dip_ce_age0/nfirms_ce_age0)+1

*gen e_ce_age`num'=(dip_ce_age`num'/(nfirms_ce_age`num'-nfirms_ce_ex_age`num'))/size0
*gen e_ce_age`num'=log(dip_ce_age`num'/(nfirms_ce_age`num'-nfirms_ce_ex_age`num'))
gen e_ce_age`num'=log(dip_ce_age`num'/(nfirms_ce_age`num'))
gen va_size_ce_age`num'=log(va_ce_age`num'/(nfirms_ce_age`num'))
}

*gen e_ce_age17=(dip_ce_age17/nfirms_ce_age17)/(dip_ce_age0/nfirms_ce_age1)
*gen e_ce_age16=(dip_ce_age16/((nfirms_ce_age15/nfirms_ce_age14)*nfirms_ce_age16))/(dip_ce_age0/nfirms_ce_age1)

*gen prova1=nfirms_ce_age0-nfirms_ce_ex_age0
*gen prova2=nfirms_ce_age1-nfirms_ce_ex_age1
*edit  South year  prova1 nfirms_ce_age1 prova2 nfirms_ce_age2 

*replace e_ce_age0=1
*replace e_ce_age17=(dip_ce_age17/(nfirms_ce_age17))/size0

*sum e_ce_age0
*stop





***************************************************************************************************
* Data set for FIRM LIFE CYCLE in North and South: average period 2007-2015
*preserve
keep South exit_ce_ex_age* exit_ce_ins_age* debt_va_ce_age* DEBT_va_ce_age* va_e_ce_age* debt_e_ce_age* DEBT_e_ce_age* e_ce_age* va_size_ce_age* year
*reshape long exit_ce_ex_age exit_ce_ins_age va_e_ce_age debt_va_ce_age DEBT_va_ce_age debt_e_ce_age DEBT_e_ce_age e_ce_age, i(South) j(age)
*replace age=20 if age==17
/*
label variable South "South=1, South=0 (North)"
label variable age "Age of Business"
rename exit_ce_ex_age Exit_Rate
label variable Exit_Rate  "Exit rate of companies"
rename exit_ce_ins_age Exit_Rate_Bankruptcy
label variable Exit_Rate_Bankruptcy  "Bankruptcy rate of companies "
rename va_e_ce_age Labour_Productivity
label variable Labour_Productivity "Productivity =1 at entry" 
rename debt_va_ce_age Leverageratio_Bank_Debt
label variable Leverageratio_Bank_Debt "Leverage ratio, Bank debt"
rename DEBT_va_ce_age Leverageratio_Total_Debt
label variable Leverageratio_Total_Debt "Leverage ratio, Total debt"
rename debt_e_ce_age Bank_debt_employee
label variable Bank_debt_employee "Bank debt per employee" 
rename DEBT_e_ce_age Total_debt_employee
label variable  Total_debt_employee "Total debt per employee"
rename e_ce_age Employment_size
label variable  Employment_size "Firm employment size (=1 at entry)"
*/

**********************************************************
*Calculate Age profile
xi i.year, noomit
capture drop _Iyear_2015 
insobs 18
replace year=99 if year==.
gen age =0 if _n==1
replace age = _n-1 if _n<=18
gen North=1-South
*1
gen Exit_RateSouth=.
gen Exit_RateNorth=.
*2
gen Leverage_TotalSouth=.
gen Leverage_TotalNorth=.
*3
gen Leverage_BankSouth=.
gen Leverage_BankNorth=.
*4
gen Exit_Rate_BankruptcyNorth=.
gen Exit_Rate_BankruptcySouth=.
*5
gen Labor_ProdNorth=.
gen Labor_ProdSouth=.
*6
gen Bankdebt_WorkerNorth=.
gen Bankdebt_WorkerSouth=.
*7
gen Totaldebt_WorkerNorth=.
gen Totaldebt_WorkerSouth=.
*8
gen Firmsize_EmpNorth=.
gen Firmsize_EmpSouth=.
*8b
gen Firmsize_VaNorth=.
gen Firmsize_VaSouth=.



foreach num in  1 2 3 4 5 6 7 8  9 10 11 12 13 14 15  {

*2nd Graph 
local numM= `num'-1
local numP=`num'+1
replace exit_ce_ex_age`num'=(exit_ce_ex_age`numM'+exit_ce_ex_age`num'+exit_ce_ex_age`numP')/3
replace exit_ce_ins_age`num'=(exit_ce_ins_age`numM'+exit_ce_ins_age`num'+exit_ce_ins_age`numP')/3
replace DEBT_va_ce_age`num'=(DEBT_va_ce_age`numM'+DEBT_va_ce_age`num'+DEBT_va_ce_age`numP')/3
replace debt_va_ce_age`num'=(debt_va_ce_age`numM'+debt_va_ce_age`num'+debt_va_ce_age`numP')/3
replace debt_va_ce_age`num'=(debt_va_ce_age`numM'+debt_va_ce_age`num'+debt_va_ce_age`numP')/3
replace debt_e_ce_age`num'=(debt_e_ce_age`numM'+debt_e_ce_age`num'+debt_e_ce_age`numP')/3
replace DEBT_e_ce_age`num'=(DEBT_e_ce_age`numM'+DEBT_e_ce_age`num'+DEBT_e_ce_age`numP')/3
replace e_ce_age`num'=(e_ce_age`numM'+e_ce_age`num'+e_ce_age`numP')/3
replace va_size_ce_age`num'=(va_size_ce_age`numM'+va_size_ce_age`num'+va_size_ce_age`numP')/3

}



*xi i.year

foreach var in  0 1 2 3 4 5 6 7 8  9 10 11 12 13 14 15 16 17 {
*reg exit_ce_ex_age`var'  South North _I*,noconst
reg exit_ce_ex_age`var'  South North, noconst
*1)
replace Exit_RateSouth=_b[South] if age==`var'
replace Exit_RateNorth=_b[North] if age==`var'
*2)
*reg exit_ce_ins_age`var'  South North _I*,noconst
reg exit_ce_ins_age`var'  South North,noconst
replace Exit_Rate_BankruptcySouth=_b[South] if age==`var'
replace Exit_Rate_BankruptcyNorth=_b[North] if age==`var'
*3)
*reg DEBT_va_ce_age`var' South North _I*,noconst
reg DEBT_va_ce_age`var' South North , noconst
replace Leverage_TotalSouth=_b[South] if age==`var'
replace Leverage_TotalNorth=_b[North] if age==`var'
*4)
*reg debt_va_ce_age`var' South North _I*,noconst
reg debt_va_ce_age`var' South North,noconst
replace Leverage_BankSouth=_b[South] if age==`var'
replace Leverage_BankNorth=_b[North] if age==`var'
*5)
*reg va_e_ce_age`var' South North _I*,noconst
reg va_e_ce_age`var' South North, noconst
replace Labor_ProdSouth=_b[South] if age==`var'
replace Labor_ProdNorth=_b[North] if age==`var'
*6)
*reg debt_e_ce_age`var' South North _I*,noconst
reg debt_e_ce_age`var' South North, noconst
replace Bankdebt_WorkerSouth=_b[South] if age==`var'
replace Bankdebt_WorkerNorth=_b[North] if age==`var' 
*7)
*reg DEBT_e_ce_age`var' South North _I*,noconst
reg DEBT_e_ce_age`var' South North,noconst
replace Totaldebt_WorkerSouth=_b[South] if age==`var'
replace Totaldebt_WorkerNorth=_b[North] if age==`var' 
*8)
*reg e_ce_age`var' South North _I*,noconst
reg e_ce_age`var' South North,noconst
replace Firmsize_EmpSouth=_b[South] if age==`var'
replace Firmsize_EmpNorth=_b[North] if age==`var'

reg va_size_ce_age`var' South North,noconst
replace Firmsize_VaSouth=_b[South] if age==`var'
replace Firmsize_VaNorth=_b[North] if age==`var'

}



replace age=20 if age==17


twoway (scatter Exit_RateNorth Exit_RateSouth age,  mcolor(red blue) msize(vlarge vlarge) )  ///  
(line Exit_RateNorth Exit_RateSouth age, ytitle("Percentage change",size(medsmall) height(4)) xtitle("Business age, years",size(medsmall) height(4)) color(red blue) lwidth(thick)  lpattern(dot dot) ///
ylabel(0 (0.02) 0.14, grid) xlabel(0 (1) 20,grid) /// 
legend(  size(medium) region(style(none)) rows(1) order(1 "North" 2 "South" ) region(fcolor(gs14)) ring(0) pos(5))) 
graph export "$figures/TotalExitRate.pdf", replace as(pdf)



twoway (scatter Exit_Rate_BankruptcyNorth Exit_Rate_BankruptcySouth age,  mcolor(red blue) msize(vlarge vlarge) )  ///  
(line Exit_Rate_BankruptcyNorth Exit_Rate_BankruptcySouth age, ytitle("Percentage change",size(medsmall) height(4)) xtitle("Business age, years",size(medsmall) height(4)) color(red blue) lwidth(thick)  lpattern(dot dot) ///
ylabel(0 (0.02) 0.1, grid) xlabel(0 (1) 20,grid) /// 
legend(  size(medium) region(style(none)) rows(1) order(1 "North" 2 "South" ) region(fcolor(gs14)) ring(0) pos(5))) 
graph export "$figures/BankruptcyExitRate.pdf", replace as(pdf)




twoway (scatter Leverage_TotalNorth Leverage_TotalSouth age,  mcolor(red blue) msize(vlarge vlarge) )  ///  
(line Leverage_TotalNorth Leverage_TotalSouth age, ytitle("Ratio",size(medsmall) height(4)) xtitle("Business age, years",size(medsmall) height(4)) color(red blue) lwidth(thick)  lpattern(dot dot) ///
ylabel(0 (0.5) 3.5, grid) xlabel(0 (1) 20,grid) /// 
legend(  size(medium) region(style(none)) rows(1) order(1 "North" 2 "South" ) region(fcolor(gs14)) ring(0) pos(5))) 
graph export "$figures/TotalLeverage.pdf", replace as(pdf)


twoway (scatter Leverage_BankNorth Leverage_BankSouth age,  mcolor(red blue) msize(vlarge vlarge) )  ///  
(line Leverage_BankNorth Leverage_BankSouth age, ytitle("ratio",size(medsmall) height(4)) xtitle("Business age, years",size(medsmall) height(4)) color(red blue) lwidth(thick)  lpattern(dot dot) ///
ylabel(0 (0.5) 3.5, grid) xlabel(0 (1) 20,grid) /// 
legend(  size(medium) region(style(none)) rows(1) order(1 "North" 2 "South" ) region(fcolor(gs14)) ring(0) pos(5))) 
graph export "$figures/BankLeverage.pdf", replace as(pdf)


twoway (scatter Firmsize_EmpNorth Firmsize_EmpSouth age,  mcolor(red blue) msize(vlarge vlarge) )  ///  
(line Firmsize_EmpNorth Firmsize_EmpSouth age, ytitle("Log",size(medsmall) height(4)) xtitle("Business age, years",size(medsmall) height(4)) color(red blue) lwidth(thick)  lpattern(dot dot) ///
ylabel(0.5 (0.5) 4, grid) xlabel(0 (1) 20,grid) /// 
legend(  size(medium) region(style(none)) rows(1) order(1 "North" 2 "South" ) region(fcolor(gs14)) ring(0) pos(5))) 
graph export "$figures/FirmSize.pdf", replace as(pdf)


twoway (scatter Firmsize_VaNorth Firmsize_VaSouth age,  mcolor(red blue) msize(vlarge vlarge) )  ///  
(line Firmsize_VaNorth Firmsize_VaSouth age, ytitle("Log",size(medsmall) height(4)) xtitle("Business age, years",size(medsmall) height(4)) color(red blue) lwidth(thick)  lpattern(dot dot) ///
ylabel(3.5 (0.5) 8.5, grid) xlabel(0 (1) 20,grid) /// 
legend(  size(medium) region(style(none)) rows(1) order(1 "North" 2 "South" ) region(fcolor(gs14)) ring(0) pos(5))) 
graph export "$figures/FirmSize.pdf", replace as(pdf)

*edit Firmsize_VaNorth Firmsize_VaSouth age
*stop



twoway (scatter Labor_ProdNorth Labor_ProdSouth  age,  mcolor(red blue) msize(vlarge vlarge) )  ///  
(line Labor_ProdNorth Labor_ProdSouth age, ytitle("Log",size(medsmall) height(4)) xtitle("Business age, years",size(medsmall) height(4)) color(red blue) lwidth(thick)  lpattern(dot dot) ///
ylabel(2 (0.5) 5, grid) xlabel(0 (1) 20,grid) /// 
legend(  size(medium) region(style(none)) rows(1) order(1 "North" 2 "South" ) region(fcolor(gs14)) ring(0) pos(5))) 
graph export "$figures/FirmProductivity.pdf", replace as(pdf)





 
drop if age==.
keep age Exit_Rate*  Exit_Rate_Bankruptcy* Exit_Rate_Bankruptcy* Leverage_Total* Leverage_Bank* Labor_Prod* Bankdebt_Worker* Totaldebt_Worker* Firmsize_Emp*
reshape long Exit_Rate Exit_Rate_Bankruptcy Leverage_Total Leverage_Bank Labor_Prod Bankdebt_Worker Totaldebt_Worker Firmsize_Emp, i(age) j(geography) string

gen South=geography=="South"

sort South age


label variable  geography "Geographical Area"
label variable  South  "South dummy"
label variable  age  "Age group"
label variable  Exit_Rate "Exit rate of all corporations"
label variable  Exit_Rate_Bankruptcy  "Exit rate with bankruptcy of all corporations"
label variable  Leverage_Total "Leverage ratio of all corporations (total debt)"
label variable  Leverage_Bank  "Leverage ratio of all corporations (bank debt)"
label variable  Labor_Prod  "Labor productivity (=1 at entry)"
label variable  Bankdebt_Worker  "Debt per worker (bank debt) (=1 at entry)"
label variable  Totaldebt_Worker  "Debt per worker (total debt) (=1 at entry)"
label variable  Firmsize_Emp "Firm employment size (=1 at entry)"

sort South age
drop geography
order South age Exit_Rate Exit_Rate_Bankruptcy Labor_Prod Leverage_Bank Leverage_Total  Bankdebt_Worker Totaldebt_Worker Firmsize_Emp
outfile using "$figures/AgeProfile.raw", replace noquote wide
export delimited "$figures/AgeProfile.tex", replace   delimiter("&")


